#include "Acceptor.h"
#include "EventLoop.h"
#include "InetAddress.h"
#include "SocketsOps.h"

#include <unistd.h>
#include <cstdio>

void sendGreeting(int sockfd, const learnmuduo::InetAddress& peerAddr)
{
  printf("[port 9981] new connection from %s",
         peerAddr.toHostPort().c_str());
  const char msg[] = "Hello from port 9981!\n";
  ::write(sockfd, msg, sizeof(msg) - 1);
  learnmuduo::sockets::close(sockfd);
}

void sendSalutation(int sockfd, const learnmuduo::InetAddress& peerAddr)
{
  printf("[port 9982] new connection from %s",
         peerAddr.toHostPort().c_str());
  const char msg[] = "Greetings from port 9982!\n";
  ::write(sockfd, msg, sizeof(msg) - 1);
  learnmuduo::sockets::close(sockfd);
}

int main()
{
  printf("main(): pid = %d\n", getpid());

  learnmuduo::EventLoop loop;

  learnmuduo::InetAddress listenAddr1(9981);
  learnmuduo::Acceptor acceptor1(&loop, listenAddr1);
  acceptor1.setNewConnectionCallback(sendGreeting);
  acceptor1.listen();

  learnmuduo::InetAddress listenAddr2(9982);
  learnmuduo::Acceptor acceptor2(&loop, listenAddr2);
  acceptor2.setNewConnectionCallback(sendSalutation);
  acceptor2.listen();

  loop.loop();
}
